package com.kool.kadmin.system.api.impl;

import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;
import com.kool.kadmin.common.util.StringUtil;
import com.kool.kadmin.common.util.ValidateUtil;
import com.kool.kadmin.system.api.SystemApi;
import com.kool.kadmin.system.api.vo.CounterVo;
import com.kool.kadmin.system.api.vo.UserVo;
import com.kool.kadmin.system.bean.SysParamDo;
import com.kool.kadmin.system.cache.UserCache;
import com.kool.kadmin.system.constant.MsgTypeEnum;
import com.kool.kadmin.system.service.*;
import com.kool.kadmin.system.vo.MessageVo;
import com.kool.kadmin.system.vo.ParamVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luyu
 */
@Component("systemApiImpl")
public class SystemApiImpl implements SystemApi {
    @Resource
    private CounterService counterService;
    @Resource
    private ParamService paramService;
    @Resource
    private UserService userService;
    @Resource
    private NotifyService notifyService;
    @Resource
    private UserCache userCache;

    @Override
    public String getCntNextValue(String cntCode) {
        return counterService.nextValue(cntCode);
    }

    @Override
    public void notifyInfo(String content, Long sender, Long receiver) {
        MessageVo messageVo = new MessageVo();
        messageVo.setSenderId(sender)
                .setReceiverId(receiver)
                .setContent(content)
                .setMsgType(MsgTypeEnum.DEV_MSG.getCode());
        notifyService.notifyInfo(messageVo);
    }

    @Override
    public ParamVo getParam(String code) {
        SysParamDo paramDo = paramService.getParamByCode(code);
        if (paramDo == null) {
            return null;
        }
        return ParamVo.parse(paramDo);
    }

    @Override
    public List<ParamVo> getSubParams(String fatherCode) {
        List<SysParamDo> paramDos = paramService.getSubParams(fatherCode);
        if (paramDos == null) {
            return null;
        }
        return paramDos.stream().map(ParamVo::parse).collect(Collectors.toList());
    }

    @Override
    public ParamVo getSubParam(String fatherCode, String key) {
        SysParamDo paramDo = paramService.getSubParam(fatherCode, key);
        if (paramDo == null) {
            return null;
        }
        return ParamVo.parse(paramDo);
    }

    @Override
    public void updateSubValue(String fatherCode, String key, String value) {
        paramService.updateSubValue(fatherCode, key, value);
    }

    @Override
    public void addParam(ParamVo paramVo) {
        ValidateUtil.valid(paramVo);
        SysParamDo paramDo = new SysParamDo();
        paramDo.setParamCode(paramVo.getParamCode())
                .setFatherCode(paramVo.getFatherCode())
                .setRootCode(paramVo.getRootCode())
                .setParamName(paramVo.getParamName())
                .setParamKey(paramVo.getParamKey())
                .setParamValue(paramVo.getParamValue())
                .setDeep(paramVo.getDeep())
                .setSeq(0);
        paramService.addParam(paramDo);
    }

    @Override
    public void updateValue(String code, String value) {
        if (StringUtil.isEmpty(code)) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "更新参数失败，参数码不能为空");
        }
        SysParamDo paramDo = new SysParamDo();
        paramDo.setParamCode(code)
                .setParamValue(value);
        paramService.updateByCode(paramDo);
    }

    @Override
    public void deleteParam(String paramCode) {
        if (StringUtil.isEmpty(paramCode)) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "接口参数错误，参数码不能为空");
        }
        paramService.deleteByCode(paramCode);
    }

    @Override
    public List<UserVo> getAllUsers() {
        return userService.getAll();
    }

    @Override
    public UserVo getUserById(Long id) {
        if (id == null) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "查询用户详情用户id不能为空");
        }
        return userService.getUserById(id);
    }

    @Override
    public UserVo getUserByName(String userName) {
        if (StringUtil.isEmpty(userName)) {
            return null;
        }
        //从缓存中查询
        UserVo userVo = userCache.getUserByName(userName);
        if (userVo == null) {
            //从数据库查询
            userVo = userService.getUserByName(userName);
            if (userVo != null) {
                userCache.setUserByName(userName, userVo);
            }
        }
        return userVo;
    }

    @Override
    public boolean initCounter(CounterVo counterVo) {
        if (counterVo == null || counterVo.getCntCode() == null) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "新增计数器失败，计数器编码不能为空");
        }
        return counterService.initCounter(counterVo);
    }


}
